#!/usr/bin/env python

import cPickle
import optparse
import sys

def main():
    '''
    hpkl_set_label.py [opts] label file.hpkl

    Set the `__label__' attribute to `label' in the histogram dictionary 
    contained in `file.hpkl'. Quotes around the label are recommended to
    preserve any LaTeX commands.

    '''

    
    op = optparse.OptionParser(usage=main.__doc__)

    opts, args = op.parse_args()

    if len(args) < 2:
        sys.stderr.write("Please supply a label and an hpkl file.\n")
        sys.stderr.write("See `hpkl_set_label.py -h' for more.\n")
        return -1

    label = args[0]
    path  = args[1]

    with open(path) as fp:
        h = cPickle.load(fp)

    h['__label__'] = label

    with open(path, 'wb') as fp:
        cPickle.dump(h, fp, cPickle.HIGHEST_PROTOCOL)

    return 0

if __name__ == "__main__":
    sys.exit(main())
